/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;
import java.util.*;
import java.io.*;
import AirLine.*;
import edu.chl.chalmers.fatclient.sb.Flight;
import java.sql.*;
import java.math.*;
/**
 *
 * @author oskar
 */
public class RandomDataCreator {

    
    
    private static Set<Airport> airportList;
    private static Set<Flight> flightList;
    private static ArrayList<Airplane> planeList;
    private static ArrayList<Airport> forFlights;
    private static int nrFlights,  nrRows,  nrColumns,  nrPlanes;
    private static String[]  alpha = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    //private String[] alpha;

    public RandomDataCreator(){


    }

    public static void initialize()
    {
        airportList = new TreeSet<Airport>();
        forFlights = new ArrayList<Airport>();
        flightList = new TreeSet<Flight>();
        planeList = new ArrayList<Airplane>();
        nrFlights = 20;
        nrPlanes = 10;
        nrRows = 3;
        nrColumns = 3;
        createAirports();
        createPlanes();
        createFlights();
    }
    
    public static Timestamp[] getTimes()
    {
        Timestamp[] ts=new Timestamp[2];
        long month=(long)30*24*60*60*1000;
        long hour=60*60*1000;
        long rdmDur=((long)(Math.random()*hour*5))+hour/3;
        long random=(long)(Math.random()*month);
        
        ts[0]=new Timestamp(System.currentTimeMillis()+random);
        ts[1]=new Timestamp(ts[0].getTime()+rdmDur);
        return ts;
    }
    
    public static String getRandomLetters(int nrLetters) {
        String s = "";
        for (int i = 0; i < nrLetters; i++) {
            s = s + alpha[(int) (Math.random() * 26)];
        }
        return s;

    }

  public static Set<Flight> getFlights()
  {
      return flightList;
  }
  
  public static Set<Airport> getAirports()
  {
      return airportList;
  }
    
  public static ArrayList<Airplane> getAirplanes()
  {
      return planeList;
  }
    
    private static void createPlanes() {
        Set<Seat> seats;
        String planeId;
        Airplane p;

        for (int i = 0; i < nrPlanes; i++) {
            seats = new HashSet<Seat>();
            planeId = getRandomLetters(8);
            for (int j = 0; j < nrRows; j++) {
                for (int k = 0; k <
                        nrColumns; k++) {
                    seats.add(new Seat(false, (int) (Math.random() * 3), planeId, j, k));
                }

            }
            p=new Airplane(planeId, seats, getRandomLetters(15));
            planeList.add(p);
        }
    }

    private static void createAirports() {
        try {
            //System.out.println(new File("woot").getAbsolutePath());
            Scanner sc = new Scanner(new File("airportlist_1.txt"));
            sc.useDelimiter("--");
            String s,
                    s2, s3;
            String name,
                    code;
            int begin, end;
            while (sc.hasNext()) {
                s = sc.next();

                //System.out.println(s.contains("/n"));
                begin =
                        s.length() - 5;
                end =
                        s.length() - 2;
                //System.out.println(s);
                name =
                        (s.subSequence(0, begin)).toString();
                code =
                        (s.subSequence(begin, end)).toString();
                airportList.add(new Airport(name, code));
                forFlights.add(new Airport(name, code));
            //System.out.println(s.subSequence(0, begin).toString());
            //System.out.println(s.subSequence(begin, end).toString());
            //System.out.println(sc.next()+"***");
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }


      private static void createFlights() {
        Flight f;
        String flightId ="";
        Airport from, to;
        int price;
        Timestamp dep, arr;
        Airplane plane;
        // String[] alpha = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

        for (int i = 0; i <
                nrFlights; i++) {
            flightId = getRandomLetters(6);
            //alpha[(int) (Math.random() * 26)] + alpha[(int) (Math.random() * 26)] + (int) (Math.random() * 10) + (int) (Math.random() * 10) + (int) (Math.random() * 10) + (int) (Math.random() * 10);

            from =
                    forFlights.get((int) (Math.random() * forFlights.size()));
            to =
                    forFlights.get((int) (Math.random() * forFlights.size()));
            price =
                    (int) (Math.random() * 10000);

            plane=planeList.get((int) (Math.random() * planeList.size()));
            
            Timestamp[] ts=RandomDataCreator.getTimes();
            dep = ts[0];
            arr=ts[1];

            f = new Flight(flightId, from, to, price, dep, arr,null, "airlineX", null, plane.getId());
                    
            flightList.add(f);
       
        }
    }
}
